package com.crscd.ai.tools;

import com.crscd.ai.entity.Appointment;
import com.crscd.ai.service.AppointmentService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 9/25/25 3:47 PM
 * @modifiedBy:
 * @version: 1.0
 */
@Slf4j
@Component
public class AppointmentTools {
  @Autowired private AppointmentService appointmentService;

  @Tool(
      name = "预约挂号",
      value =
          "根据参数，先执行工具方法queryDepartment查询是否可预约，并直接给用户回答是否可以预约，并让用户确认所有预约信息，用户确认后再进行预约.如果用户没有提供具体的医生姓名，请从向量存储中找到一位医生。")
  public String bookAppointment(Appointment appointment) {
    final Appointment appointmentDb = appointmentService.getOne(appointment);
    if (Objects.isNull(appointmentDb)) {
      appointment.setId(null);
      if (appointmentService.save(appointment)) {
        return "预约成功，并返回预约详情";
      } else {
        return "预约失败";
      }
    }
    return "您在相同的科室和时间已有预约";
  }

  @Tool(name = "取消挂号", value = "根据参数，查询预约是否存在，如果存在则删除预约记录并返回'取消预约成功'，否则返回'取消预约失败'")
  public String cancelAppointment(Appointment appointment) {
    final Appointment appointmentDb = appointmentService.getOne(appointment);
    if (Objects.isNull(appointmentDb)) {
      return "您没有预约记录，请核对预约科室和时间";
    }
    final boolean removed = appointmentService.removeById(appointmentDb.getId());
    return removed ? "取消预约成功" : "取消预约失败";
  }

  @Tool(name = "查询是否有号源", value = "根据科室名称，日期，时间和医生查询是否有号源，并返回给用户")
  public boolean queryDepartment(
      @P(value = "科室名称") String name,
      @P(value = "日期") String date,
      @P(value = "时间，可选值：上午、下午") String time,
      @P(value = "医生名称", required = false) String doctorName) {
    log.info("查询是否有号源");
    log.info("科室名称：{}", name);
    log.info("日期：{}", date);
    log.info("时间：{}", time);
    log.info("医生名称：{}", doctorName);

    return true;
  }
}
